package com.victualler.product.vender.service.impl;

import com.victualler.product.vender.dto.VenderInfoDto;
import com.victualler.product.vender.entity.VenderImgDetailDao;
import com.victualler.product.vender.entity.VenderItemDetailDao;
import com.victualler.product.vender.entity.VenderMainDao;
import com.victualler.product.vender.repository.VenderMainQueryRepository;
import com.victualler.product.vender.repository.VenderMainRepository;
import com.victualler.product.vender.service.VenderImgDetailService;
import com.victualler.product.vender.service.VenderInfoService;
import com.victualler.product.vender.service.VenderItemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Guoshichang
 * @Date: Create in 23:41 2018/4/9
 * @Discription:    对大对象进行操作
 */
@Service
public class VenderInfoServiceImpl implements VenderInfoService {

     @Autowired
     private VenderMainRepository venderMainRepository;

    @Autowired
    private VenderMainQueryRepository venderMainQueryRepository;

     @Autowired
     private VenderImgDetailService venderImgDetailService;
     @Autowired
     private VenderItemDetailService venderItemDetailService;
    /**
     * 根据id获取对应对象
     * @param id
     * @return
     */
    @Override
    public VenderInfoDto getById(Long id) {
        VenderMainDao main = venderMainRepository.getOne(id);
        List<VenderImgDetailDao> imgs = venderImgDetailService.getByParentId(id);
        List<VenderItemDetailDao> items = venderItemDetailService.getByParentId(id);
        return new VenderInfoDto(main,imgs,items);
    }



    /**
     * 保存组合厂商对象
     * @param venderInfoDto
     */
    @Override
    @Transient
    public void save(VenderInfoDto venderInfoDto) {
        for(VenderImgDetailDao venderImgDetailDao : venderInfoDto.getVenderImgDetailDaoList()){
            if(venderImgDetailDao.getPlace() == 0){ //主图
                venderInfoDto.getVenderMainDao().setImgId(venderImgDetailDao.getImgId());
                break;
            }
        }
        venderMainRepository.save(venderInfoDto.getVenderMainDao());
        //更新子表ID
        for(VenderImgDetailDao venderImgDetailDao : venderInfoDto.getVenderImgDetailDaoList()){
            venderImgDetailDao.setParentId(venderInfoDto.getVenderMainDao().getId());
        }

        for(VenderItemDetailDao venderItemDetailDao : venderInfoDto.getVenderItemDetailDaoList()){
            venderItemDetailDao.setParentId(venderInfoDto.getVenderMainDao().getId());
        }
        venderImgDetailService.saveAll(venderInfoDto.getVenderImgDetailDaoList());
        venderItemDetailService.saveAll(venderInfoDto.getVenderItemDetailDaoList());
    }

    @Override
    @Transient
    public void updateVender(VenderInfoDto venderInfoDto) {
        for(VenderImgDetailDao venderImgDetailDao : venderInfoDto.getVenderImgDetailDaoList()){
            if(venderImgDetailDao.getPlace() == 0){ //主图
                venderInfoDto.getVenderMainDao().setImgId(venderImgDetailDao.getImgId());
                break;
            }
        }
        Long parentId=venderInfoDto.getVenderMainDao().getId();
        venderMainRepository.save(venderInfoDto.getVenderMainDao());
        venderImgDetailService.updateList(venderInfoDto.getVenderImgDetailDaoList(),parentId);
        venderItemDetailService.updateList(venderInfoDto.getVenderItemDetailDaoList(),parentId);
    }


}
